package com.zh.device.controller;

import com.zh.common.utils.PageUtils;
import com.zh.common.utils.Query;
import com.zh.common.utils.R;
import com.zh.device.domain.HardwareDO;
import com.zh.device.service.HardwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author madara
 * @email 54925881@qq.com
 * @date 2019-08-06 10:52:07
 */
 
@Controller
@RequestMapping("/device/hardware")
public class HardwareController {
	@Autowired
	private HardwareService hardwareService;
	
	@GetMapping()
	String Hardware(){
	    return "device/hardware/hardware";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<HardwareDO> hardwareList = hardwareService.list(query);
		int total = hardwareService.count(query);
		PageUtils pageUtils = new PageUtils(hardwareList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "device/hardware/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		HardwareDO hardware = hardwareService.get(id);
		model.addAttribute("hardware", hardware);
	    return "device/hardware/edit";
	}

	@ResponseBody
	@GetMapping("/getHardware/{id}")
	HardwareDO getHardware(@PathVariable("id") Integer id){
	    return  hardwareService.get(id);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( HardwareDO hardware){
		if(hardwareService.save(hardware)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( HardwareDO hardware){
		hardwareService.update(hardware);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer id){
		if(hardwareService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] ids){
		hardwareService.batchRemove(ids);
		return R.ok();
	}
	
}
